<template>
  <div id="">
    <headerbox :hasBack='true' :headerRightTxt="'发布'" @headerRightEvent="publishEvent"></headerbox>
    <headerTab :headerArr="headerArr" @tabEvent="tabEvent"></headerTab>

    <v-scroll :on-refresh="onRefresh" :on-infinite="enableInfinite ? onInfinite : undefined" style="bottom:0;">
      <div v-for="matchInfo in matchList" :key="matchInfo.key">
        <activityItem
        :matchInfo="matchInfo"></activityItem>
      </div>
    </v-scroll>
    <router-view class="childrenPage"></router-view>
  </div>
</template>
<script>
import {toChildrenPage} from '@/utils/util.js';
import {matchMyPush,matchMyPartake} from '@/utils/ajaxRequest.js';
import vScroll from '@/components/loadMore/loadMore';
export default {
  name: "",
  data: () => ({
    headerArr:['发布','参加'],
    token:'',
    matchList:'',   //活动列表
    idx:0,      //0：发布；1：参加
    pageNo:1,   //分页
    enableInfinite:true,
  }),
  components: {
    headerTab:()=>import('../template/headerTab'),
    activityItem:()=>import('./activityItem'),
    vScroll,
  },
  methods: {
    onRefresh(done) {   //刷新
      this.pageNo = 1;
       this.getMatchMypush();
       done() // call done
    },
    onInfinite(done) {    //加载
        let self = this;
        self.enableInfinite = false;
        this.pageNo++;
        this.getMatchMypush();
        done();
     },
    // 发布页面
    publishEvent(data) {
      toChildrenPage(this,'activityPublish');
    },
    tabEvent(data) {
      console.log(data);
      this.pageNo = 1;
      this.getMatchMypush(data);
    },
    // 获取我发布的赛事
    getMatchMypush(idx) {
      let self = this;
      let matchToken = {
        token:self.token,
        currentPage:self.pageNo,
      }
      if (idx==0) {   //我发布的赛事
        matchMyPush(matchToken).then(res=>{
          console.log(res);
          if(res.data.code==200){
            let result = res.data.result;
            if (result.length<1) {
              self.enableInfinite = false;    //关闭上拉加载
            }else{
              self.enableInfinite = true;     //打开上拉加载
            }
            if (self.pageNo==1) {
              self.matchList = result;
            }else{
              self.matchList = self.matchList.concat(result);
              console.log(self.matchList);
            }
          }
        });
      }else if (idx==1){    //我参加的赛事
        matchMyPartake(matchToken).then(res=>{
          console.log(res);
          if(res.data.code==200){
            let result = res.data.result;
            if (result.length<1) {
              self.enableInfinite = false;    //关闭上拉加载
            }else{
              self.enableInfinite = true;     //打开上拉加载
            }
            if (self.pageNo==1) {
              self.matchList = result;
            }else{
              self.matchList = self.matchList.concat(result);
              console.log(self.matchList);
            }
          }
        });
      }
    }
  },
  mounted() {
    //do something after mounting vue instance
    let xlyd_loginInfo = JSON.parse(window.localStorage.getItem('xlyd_loginInfo'));
    this.token = xlyd_loginInfo.token;
    this.getMatchMypush(0);
  }
}
</script>
<style lang="scss" scoped>
</style>
